<?php

namespace app\common\domain;

class Warehouse
{
    /**
     * 构建货架分层数据
     * @param int $shelfNo 货架号
     * @param int $totalLayers 总层数
     * @param int $area 区域（1顺丰/2京东）
     * @return array
     */
    public static function buildShelfData(int $shelfNo, int $totalLayers, int $area): array
    {
        $shelfData = [];
        for ($layer = 1; $layer <= $totalLayers; $layer++) {
            $shelfData[] = [
                'area'        => $area,
                'shelfno'    => $shelfNo,
                'layerno'     => $layer,
                'layertype'   => self::getLayerType($layer),
                'max_num'     => 0, // 从0开始计数
                'is_locked'   => 1  // 默认禁用
            ];
        }
        return $shelfData;
    }

    /**
     * 根据层数分配货架类型
     */
    private static function getLayerType(int $layerNo): int
    {
        $typeMap = [
            1 => 1, // 键值为1是小件
            2 => 1, // 键值为1是小件
            3 => 2, // 键值为2是中件
        ];
        return $typeMap[$layerNo] ?? 3; // 默认返回3（大件）
    }

    /**
     * 构建分页参数查询条件
     * @param array $params
     * @return array
     */
    public function getListCondition(array $params = []){
        // 构建查询条件
        $where = [];
        if (!empty($params['area']))$where[] = ['area', '=', $params['area']];//区域
        if (!empty($params['shelfno']))$where[] = ['shelfno', '=', $params['shelfno']];//货架号
        if (!empty($params['layerno']))$where[] = ['layerno', '=', $params['layerno']];//货架层
        if (!empty($params['layertype']))$where[] = ['layertype', '=', $params['layertype']];//货架层类型1是小件，2是中件，3是大件
        if (!empty($params['is_locked']))$where[] = ['is_locked', '=', $params['is_locked']];//状态

        // 分页参数
        $page = $params['page'] ?? 1;
        $pageSize = $params['page_size'] ?? 10;
        // 排序参数
        $order = $params['order'] ?? 'id DESC';
        return ['where'=>$where,'page'=> $page,'pageSize'=> $pageSize, 'order'=>$order];

    }

    /**
     * 格式化缓存数据
     * @param object $activeLayer
     * @return array
     */
    public static function formatCacheData(object $activeLayer){
        return [
            'id' => $activeLayer->id,
            'area' => $activeLayer->area,
            'shelfno' => $activeLayer->shelfno,
            'layerno' => $activeLayer->layerno,
            'layertype' => $activeLayer->layertype,
            'max_num' => $activeLayer->max_num
        ];
    }

    /**
     * 过滤只保留允许修改的字段
     * @param array $params
     * @return array
     */
    public function filterField(array $params = []): array
    {
        // 过滤只保留允许修改的字段
        return array_intersect_key($params, array_flip([
            'area', 'shelfno', 'layerno', 'layertype', 'is_locked'
        ]));
    }

    /**
     * 构建检查货架位置是否唯一
     * @param array $data
     * @param object $warehouse
     * @return array
     */
    public function getUniqueCond(array $data = [],object $warehouse): array
    {
        $uniqueCond['area'] = $data['area'] ?? $warehouse->area;
        $uniqueCond['shelfno'] = $data['shelfno'] ?? $warehouse->shelfno;
        $uniqueCond['layerno'] = $data['layerno'] ?? $warehouse->layerno;
        return $uniqueCond;
    }

}